package com.dmwork.service.interaction.controller.front.me;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmwork.common.base.result.R;
import com.dmwork.common.base.util.JwtInfo;
import com.dmwork.common.base.util.JwtUtils;
import com.dmwork.service.interaction.entity.FeedComment;
import com.dmwork.service.interaction.entity.FeedCommentLike;
import com.dmwork.service.interaction.mq.sender.MessageSender;
import com.dmwork.service.interaction.service.FeedCommentLikeService;
import com.dmwork.service.interaction.service.FeedCommentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * <p>
 * 动态评论的点赞记录表 前端控制器
 * </p>
 *
 * @author 李天
 * @since 2021-05-07
 */
@Transactional
@RestController
@RequestMapping("/front/dm_work/interaction/feed_comment_like/me")
public class FrontMeFeedCommentLikeController {

    @Autowired
    private FeedCommentLikeService feedCommentLikeService;
    @Autowired
    private FeedCommentService feedCommentService;
    @Autowired
    private MessageSender messageSender;

    @ApiOperation("为指定评论点赞或取消点赞")
    @PutMapping("like/{commentId}/{like}")
    public R like(HttpServletRequest request, @PathVariable String commentId, @PathVariable Integer like) {
        JwtInfo jwtInfo = JwtUtils.getCustomerInfoByJwtToken(request);
        //查出评论记录详情
        FeedComment feedComment = feedCommentService.getById(commentId);
        if (like == -1) { //如果like == -1，说明是取消点赞，要删除点赞记录表中对应的点赞记录，并将原来点赞的评论记录中like_count - 1
            QueryWrapper<FeedCommentLike> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("customer_id", jwtInfo.getId());
            queryWrapper.eq("comment_id", commentId);
            FeedCommentLike feedCommentLike = feedCommentLikeService.getOne(queryWrapper);
            feedCommentLikeService.removeById(feedCommentLike.getId());
            //更新评论记录中的like_count
            feedCommentService.decreaseLikeCount(commentId);
            String customerId = feedComment.getCustomerId();
            if (!customerId.equals(jwtInfo.getId())) {
                messageSender.sendCommentLikeMessage(customerId, feedCommentLike.getId(), 1, 0);
            }
            return R.ok().message("取消点赞成功");
        } else { //如果like == 1，说明是点赞，要添加一条点赞记录，并通过MQ为消息系统投递一条消息，让消息系统为被点赞的用户创建一条消息
            //增加一条点赞记录
            FeedCommentLike feedCommentLike = new FeedCommentLike();
            feedCommentLike.setCustomerId(jwtInfo.getId());
            feedCommentLike.setCommentId(commentId);
            feedCommentLikeService.save(feedCommentLike);
            //对应评论的like_count + 1
            feedCommentService.increaseLikeCount(commentId);
            //向MQ投递消息
            String customerId = feedComment.getCustomerId();
            if (!customerId.equals(jwtInfo.getId())) {
                messageSender.sendCommentLikeMessage(customerId, feedCommentLike.getId(), 1, 1);
            }
            return R.ok().message("点赞成功");
        }
    }

}

